<?php

/*
* @Author: 玛瑙
* @Date:   2018-04-21 17:46:50
* @Last Modified by:   Administrator
* @Last Modified time: 2018-05-28 13:14:07
*/
namespace app\company\controller;

class Order extends Common {
	/**
	 *  列表页
	 */
	public function index() {

		$data = input('post.');

		if ($data['is_distribut'] != '') {
			$map['is_distribut'] = $data['is_distribut'];
		}

                 $map['order_status'] = 3;
                if($data['order_sn'] != ''){
                  $map['order_sn'] = ['like',"%{$data['order_sn']}%"];
                }

//                dump(strtotime($data['start_time']));die;
		if(!empty($data['start_time']) && !empty($data['end_time'])) {
			$map['finish_time'] = ['between time',[strtotime($data['start_time']), strtotime($data['end_time'])+86399]];
		} elseif (!empty($data['start_time']) && empty($data['end_time'])) {
			$map['finish_time'] = ['between time',[strtotime($data['start_time']), time()]];
		} elseif (empty($data['start_time']) && !empty($data['end_time'])) {
			$map['finish_time'] = ['between time',[0,strtotime($data['end_time'])+86399]];
		}

		//页码
		$pageNum = 1;
		if (is_numeric(input('post.pageNum'))) {
			$pageNum = input('post.pageNum');
		}
        //每页数量
		$numPerPage = 20;
		$data['numPerPage'] = $numPerPage;

		$map['company_id'] = session('company.company_id');
		$list = db('order')
                    ->where($map)
                    ->order('update_time desc')
                    ->page($pageNum, $numPerPage)
                    ->select();
//                dump($list = db('order')->getLastSql());
		$count = get_list('order',$map,'','', '','1');
                $sum = db("order")->where($map)->sum("total_amount");
                $data['price'] = number_format($sum,2);
		$data['list'] = $list;
		$data['count'] = $count;

		return view('',$data);
	}

	/**
	 * 查看订单详情
	 */
	public function detail() {
		$data = [];
        $order_id = input('get.id');
        $reback = db('order')->field('open_id')->where('order_id',$order_id)->find();
        $map['order_id'] = $order_id;
        $data['dl_info'] = get_info('driving_license', $map, '');
        $data['order_info'] = get_info('order', array('order_id' => $order_id), '');
        return view('', $data);
	}

	/**
	 * 修改订单状态
	 */
	public function modify() {
		$data = input('get.');
		try {
			doModify('order',$data);
		} catch (Exception $e) {
			return json([
				'statusCode' =>300,
				'message' =>"失败"
				]);
		}

		return json([
			'statusCode' =>200,
			'message' => "成功",
			'navTabId' => 'order_manage',
			]);
	}

	 /**
     * 订单打印
     */
	 public function orderprint() {

	 	$map['order_id'] = input('id');

	 	$list = get_info('order',$map,'');

	 	$company_id = $_SESSION['think']['company']['company_id'];
	 	if ($list['company_id'] != $company_id) {
	 		return json([
	 			'statusCode' => 300,
	 			'message' => '没有权限',
	 			]);
	 	}

	 	$this->assign('list', $list);

	 	return $this->fetch('orderprint');
	 }
	}